package cn.lx.es.query.utils;

import cn.lx.es.query.annotation.ESID;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: esdemo
 * @description: 工具类
 * @author: X-Pacific zhang
 * @create: 2019-01-18 16:23
 **/
public class Tools {

    private Tools() {
    }

    /**
     * 根据对象中的注解获取ID的字段值
     *
     * @param obj
     * @return
     */
    public static String getESId(Object obj) throws Exception {
        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field f : fields) {
            ReflectionUtils.makeAccessible(f);
            ESID esid = f.getAnnotation(ESID.class);
            if (esid != null) {
                Object value = f.get(obj);
                if (value == null) {
                    return null;
                } else {
                    return value.toString();
                }
            }
        }
        return null;
    }

    /**
     * 获取o中所有的字段有值的map组合
     *
     * @return
     */
    public static Map getFieldValue(Object o) throws IllegalAccessException {
        Map retMap = new HashMap();
        Field[] fs = o.getClass().getDeclaredFields();
        for (int i = 0; i < fs.length; i++) {
            Field f = fs[i];
            ReflectionUtils.makeAccessible(f);
            if (f.get(o) != null) {
                retMap.put(f.getName(), f.get(o));
            }
        }
        return retMap;
    }

    public static boolean arrayISNULL(Object[] objs) {
        if (objs == null || objs.length == 0) {
            return true;
        }
        boolean flag = false;
        for (int i = 0; i < objs.length; i++) {
            if (!StringUtils.isEmpty(objs[i])) {
                flag = true;
            }
        }
        if (flag) {
            return false;
        } else {
            return true;
        }
    }

    final static String SEPARATOR = "|";

    /**
     * 一个查询字段多个值查询拼接
     *
     * @param data
     * @return
     */
    public static String mutilIntegerToString(List<Integer> data) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < data.size(); i++) {
            sb.append(data.get(i));
            if (i != (data.size() - 1)) {
                sb.append(SEPARATOR);
            }
        }
        return sb.toString();
    }

    /**
     * 一个查询字段多个值查询拼接
     *
     * @param data
     * @return
     */
    public static String listToString(List<String> data) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < data.size(); i++) {
            sb.append(data.get(i));
            if (i != (data.size() - 1)) {
                sb.append(SEPARATOR);
            }
        }
        return sb.toString();
    }

    /**
     * 一个查询字段多个值查询拼接
     *
     * @param data
     * @return
     */
    public static String mutilLongToString(List<Long> data) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < data.size(); i++) {
            sb.append(data.get(i));
            if (i != (data.size() - 1)) {
                sb.append(SEPARATOR);
            }
        }
        return sb.toString();
    }

    /**
     * 一个查询字段多个值查询拼接
     *
     * @param data
     * @return
     */
    public static String mutilDateToString(List<Date> data) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < data.size(); i++) {
            sb.append(data.get(i).getTime());
            if (i != (data.size() - 1)) {
                sb.append(SEPARATOR);
            }
        }
        return sb.toString();
    }

    public static String mutilStringToString(List<String> data) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < data.size(); i++) {
            sb.append(data.get(i));
            if (i != (data.size() - 1)) {
                sb.append(SEPARATOR);
            }
        }
        return sb.toString();
    }

}
